package org.br.erp.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.br.erp.base.entity.Warehouse;
import org.br.erp.base.mapper.WarehouseMapper;
import org.br.erp.base.service.IWarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

/**
 * @Description: 仓库
 * @Author: jeecg-boot
 * @Date:   2022-08-27
 * @Version: V1.0
 */
@Service
public class WarehouseServiceImpl extends ServiceImpl<WarehouseMapper, Warehouse> implements IWarehouseService {

    @Autowired
    private WarehouseMapper warehouseMapper;

    @Override
    public Warehouse getWWWarehouseByCompanyId(String companyid, String ww,String vendorid,String customerid) {
        QueryWrapper<Warehouse> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("companyid",companyid);
        if(ww.equalsIgnoreCase("0")){
            //普通库
        }else if(ww.equalsIgnoreCase("1")){
            //供应商库
            queryWrapper.and(item->item.eq("vendorid",vendorid).or(i->i.isNull("vendorid")));
            queryWrapper.eq("whtype","1");
        }else if(ww.equalsIgnoreCase("2")){
            queryWrapper.and(item->item.eq("customerid",vendorid).or(i->i.isNull("customerid")));
            queryWrapper.eq("whtype","2");
        }

        List<Warehouse> warehouses = warehouseMapper.selectList(queryWrapper);
        if(warehouses!=null&& warehouses.size()>0){
            for (Warehouse warehouse : warehouses) {
                if(warehouse.getVendorid()!=null && warehouse.getVendorid().equalsIgnoreCase(vendorid)){
                    return warehouse;
                }else if(warehouse.getCustomerid()!=null && warehouse.getCustomerid().equalsIgnoreCase(customerid)){
                    return warehouse;
                }
            }
            return warehouses.get(0);
        }
        return null;
    }

    @Override
    public Warehouse getWarehouseByWarehousename(String warehousename) {
        QueryWrapper<Warehouse> queryWrapper=new QueryWrapper<>();
        queryWrapper.like("name",warehousename);
        List<Warehouse> warehouses = warehouseMapper.selectList(queryWrapper);
        if(warehouses.size()>0){
            return warehouses.get(0);
        }else{
            return null;
        }
    }

    @Override
    public boolean checkWarehouseHasStocklocation(String warehouseid) {
        Warehouse warehouse = warehouseMapper.selectById(warehouseid);
        if(warehouse!=null && warehouse.getIslocation()!=null && warehouse.getIslocation().equalsIgnoreCase("true")){
            return true;
        }else{
            return false;
        }
    }
}
